package com.kot.botrunningsystem.utils;

import java.util.*;

//这里面的内容实际上没有用，因为最后都会被bot代码给覆盖掉，在这里写，是因为idea自动补全好些，写完再复制到前端写bot代码的地方
public class Bot implements com.kot.botrunningsystem.utils.BotInterface {//在这先写一个智能一点的ai

    int ind = 0;
    Random random = new Random();
    static final int MAXN = 25;
     int n, m;
     boolean[][] vis = new boolean[MAXN][MAXN];
    static int[] X = {1, 0, 0, -1}; //这两个数组中元素的位置是固定的，不能乱放
    static int[] Y = {0, -1, 1, 0};
     int[][] Maze = new int[MAXN][MAXN];
//     String[] strs = new String[MAXN];

    Integer startx ;
    Integer starty;
    Integer endx ;
    Integer endy ;



    static class Node {
        int x, y;
        Node(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
     Node[][] pre = new Node[MAXN][MAXN];//记录前驱结点
     List<Node> vec = new ArrayList<>(); //方便最后输出
     boolean judge(int x, int y) {
        if (x < 0 || x >= 20 || y < 0 || y >= 21) return false;
        if (vis[x][y] == true) return false;
        if (Maze[x][y] == 1) return false;
        return true;
    }
     void BFS(Node s) {
        Queue<Node> q = new LinkedList<>();
        q.offer(s);
        vis[s.x][s.y] = true;
        while (!q.isEmpty()) {
            Node top = q.poll();
            for (int i = 0; i < 4; i++) {
                int newX = top.x + X[i];
                int newY = top.y + Y[i];
                if (judge(newX, newY)) {
                    vis[newX][newY] = true;
                    Node node = new Node(newX, newY);
                    pre[newX][newY] = top; //记录前驱结点
                    q.offer(node);
                }
                if (newX == endx && newY == endy) {
                    break;
                }
            }
        }
    }
    //将坐标信息从按从起点到终点存入vector中
     void DFS(int x, int y) {
        if (x == startx && y == starty) {
            Node node = new Node(x, y);
            vec.add(node);
            return;
        }
        DFS(pre[x][y].x, pre[x][y].y);
        Node node = new Node(x, y);
        vec.add(node);
    }

    //根据相邻坐标的位置信息打印如何走
     Integer print() {

        Node next = vec.get( 1);
        Node now = vec.get(0);

        if (next.x > now.x) return 2;
        else if (next.x < now.x) return 0;
        else if (next.y > now.y) return 1;
        else return 3;

    }



    @Override
    public Integer nextMove(String input) {

         if(input==null || input.equals(""))return 0;//model每20帧才穿一次，所以刚开始可能为空

         for(int r=0;r<21;r++){
             for(int c=0;c<20;c++){
                 Maze[r][c]=0;
                 if(r==10 || r<4 || r>16)continue;
                 if(r<10){
                     if(c%2==1)Maze[r][c]=1;
                 }else {
                     if(c%2==0)Maze[r][c]=1;
                 }
             }
         }
        System.out.println("input="+input);
         String[] str = input.split("#");
        System.out.println(Arrays.toString(str));
        startx=Double.valueOf(str[2]).intValue();//小数字符串需要先转小数再转整数

        System.out.println(startx);
        starty=Double.valueOf(str[3]).intValue();

         endx = Double.valueOf(str[5]).intValue();
         endy = Double.valueOf(str[6]).intValue();
         BFS(new Node(startx,starty));
         DFS(endx,endy);
        return print();

    }
}
